#include <system_LPC17xx.h>
#include <LPC17xx.h>
#include <lpc17xx_clkpwr.h>
#include <lpc17xx_uart.h>

void Uart0Cfg(uint32_t ulBaudRate,UART_PARITY_Type eParity)
{
	UART_CFG_Type      Uart0CfgVal;    //Uart0 boundry ,data format,enable
	UART_FIFO_CFG_Type Uart0FRCVal;    //FIFO config
		
	//APB Clock Config
	CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_UART0, CLKPWR_PCLKSEL_CCLK_DIV_4);
	
	//Data format and baudry config
	Uart0CfgVal.Baud_rate = ulBaudRate;
	Uart0CfgVal.Databits  = UART_DATABIT_8;
	Uart0CfgVal.Parity    = eParity;
	Uart0CfgVal.Stopbits  = UART_STOPBIT_1;
	UART_Init(LPC_UART0, &Uart0CfgVal);
	
	//FIFO Config , the flowing function has enabled FIFO, so needn't enable it visible*/
	Uart0FRCVal.FIFO_ResetRxBuf = DISABLE;
	Uart0FRCVal.FIFO_ResetTxBuf = DISABLE;
	Uart0FRCVal.FIFO_Level      = UART_FIFO_TRGLEV2;
	Uart0FRCVal.FIFO_DMAMode    = DISABLE;
	UART_FIFOConfig(LPC_UART0,&Uart0FRCVal);
	
	//Int Config
	UART_IntConfig(LPC_UART0,UART_INTCFG_RBR,ENABLE);
	UART_IntConfig(LPC_UART0,UART_INTCFG_THRE,DISABLE);
	UART_IntConfig(LPC_UART0,UART_INTCFG_RLS,DISABLE);
	//UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1,UART1_INTCFG_MS,DISABLE);  //Only for Uart1
	//UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1,UART1_INTCFG_CTS,DISABLE); //Only for Uart1
	UART_IntConfig(LPC_UART0,UART_INTCFG_ABEO,DISABLE);
	UART_IntConfig(LPC_UART0,UART_INTCFG_ABTO,DISABLE);
	
	UART_TxCmd(LPC_UART0,ENABLE);                  //Enable Tx
	
	//NVIC Config
	NVIC_ClearPendingIRQ(UART0_IRQn);              //Clear Pending
	NVIC_SetPriority(UART0_IRQn, 1<<5);            //Priority
	NVIC_EnableIRQ(UART0_IRQn);                    //Enable NVIC
}

/*---------------------------------------------------------------------------------------*/
void Uart2Cfg(uint32_t ulBaudRate,UART_PARITY_Type eParity)
{
	UART_CFG_Type      Uart2CfgVal;    //Uart2 boundry ,data format,enable
	UART_FIFO_CFG_Type Uart2FRCVal;    //FIFO config
	
	//APB Clock Config
	CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_UART2, CLKPWR_PCLKSEL_CCLK_DIV_4);
	
	//Data format and baudry config 
	Uart2CfgVal.Baud_rate = ulBaudRate;
	Uart2CfgVal.Databits  = UART_DATABIT_8;
	Uart2CfgVal.Parity    = eParity;
	Uart2CfgVal.Stopbits  = UART_STOPBIT_1;
	UART_Init(LPC_UART2, &Uart2CfgVal);
	
	//FIFO Config , the flowing function has enabled FIFO, so needn't enable it visible
	Uart2FRCVal.FIFO_ResetRxBuf = DISABLE;
	Uart2FRCVal.FIFO_ResetTxBuf = DISABLE;
	Uart2FRCVal.FIFO_Level      = UART_FIFO_TRGLEV0;
	Uart2FRCVal.FIFO_DMAMode    = DISABLE;
	UART_FIFOConfig(LPC_UART2,&Uart2FRCVal);
	
	//Int Config
	UART_IntConfig(LPC_UART2,UART_INTCFG_RBR,ENABLE);
	UART_IntConfig(LPC_UART2,UART_INTCFG_THRE,DISABLE);
	UART_IntConfig(LPC_UART2,UART_INTCFG_RLS,DISABLE);
	//UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1,UART1_INTCFG_MS,DISABLE);  //Only for Uart1
	//UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1,UART1_INTCFG_CTS,DISABLE); //Only for Uart1
	UART_IntConfig(LPC_UART2,UART_INTCFG_ABEO,DISABLE);
	UART_IntConfig(LPC_UART2,UART_INTCFG_ABTO,DISABLE);
	
	UART_TxCmd(LPC_UART2,ENABLE);                  //Enable Tx
	
	//NVIC Config
	NVIC_ClearPendingIRQ(UART2_IRQn);              //Clear Pending
	NVIC_SetPriority(UART2_IRQn, 1<<5);            //Priority
	NVIC_EnableIRQ(UART2_IRQn);                    //Enable NVIC
}

void Uart3Cfg(uint32_t ulBaudRate,UART_PARITY_Type eParity)
{
	UART_CFG_Type      Uart3CfgVal;    //Uart3 boundry ,data format,enable
	UART_FIFO_CFG_Type Uart3FRCVal;    //FIFO config
	
	
	//APB Clock Config
	CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_UART3, CLKPWR_PCLKSEL_CCLK_DIV_4);
	
	//Data format and baudry config 
	Uart3CfgVal.Baud_rate = ulBaudRate;
	Uart3CfgVal.Databits  = UART_DATABIT_8;
	Uart3CfgVal.Parity    = eParity;
	Uart3CfgVal.Stopbits  = UART_STOPBIT_1;
	UART_Init(LPC_UART3, &Uart3CfgVal);
	
	LPC_UART3->ICR = 0;
	
	//FIFO Config , the flowing function has enabled FIFO, so needn't enable it visible
	Uart3FRCVal.FIFO_ResetRxBuf = DISABLE;
	Uart3FRCVal.FIFO_ResetTxBuf = DISABLE;
	Uart3FRCVal.FIFO_Level      = UART_FIFO_TRGLEV2;
	Uart3FRCVal.FIFO_DMAMode    = DISABLE;
	UART_FIFOConfig(LPC_UART3,&Uart3FRCVal);
	
	//Int Config
	UART_IntConfig(LPC_UART3,UART_INTCFG_RBR,ENABLE);
	UART_IntConfig(LPC_UART3,UART_INTCFG_THRE,DISABLE);
	UART_IntConfig(LPC_UART3,UART_INTCFG_RLS,DISABLE);
	//UART_IntConfig((LPC_UART_TypeDef *)LPC_UART0,UART0_INTCFG_MS,DISABLE);  //Only for Uart1
	//UART_IntConfig((LPC_UART_TypeDef *)LPC_UART0,UART0_INTCFG_CTS,DISABLE); //Only for Uart1
	UART_IntConfig(LPC_UART3,UART_INTCFG_ABEO,DISABLE);
	UART_IntConfig(LPC_UART3,UART_INTCFG_ABTO,DISABLE);
	
	UART_TxCmd(LPC_UART3,ENABLE);                  //Enable Tx
	
	//NVIC Config
	NVIC_ClearPendingIRQ(UART3_IRQn);              //Clear Pending
	NVIC_SetPriority(UART3_IRQn, 1<<5);            //Priority
	NVIC_EnableIRQ(UART3_IRQn);                    //Enable NVIC
}